package com.ioc.business.order.controller;


import cn.hutool.core.util.ObjectUtil;
import com.ioc.business.order.domain.AggregativeIndicator;
import com.ioc.business.order.domain.bo.aggregative.indicator.AggregativeIndicatorBo;
import com.ioc.business.order.domain.bo.aggregative.indicator.AggregativeIndicatorQueryBo;
import com.ioc.business.order.service.IAggregativeIndicatorService;
import com.ioc.business.order.vo.aggregative.indicator.AggregativeIndicatorImportVo;
import com.ioc.business.order.vo.aggregative.indicator.AggregativeIndicatorPageVo;
import com.ioc.business.order.vo.aggregative.indicator.AggregativeIndicatorVo;
import com.ioc.common.annotation.Log;
import com.ioc.common.core.controller.BaseController;
import com.ioc.common.core.domain.AjaxResult;
import com.ioc.common.core.page.TableDataInfo;
import com.ioc.common.core.page.TableVo;
import com.ioc.common.enums.BusinessType;
import com.ioc.common.utils.poi.ExcelUtil;
import com.ioc.common.validate.AddGroup;
import com.ioc.common.validate.EditGroup;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 综合指标表 前端控制器
 * </p>
 *
 * @author WeiZhongRong
 * @since 2025-01-07
 */
@RestController
@RequestMapping("/aggregative-indicator")
@AllArgsConstructor
public class AggregativeIndicatorController extends BaseController {

    private final IAggregativeIndicatorService aggregativeIndicatorService;

    /**
     * 分页查询综合指标列表
     *
     * @param queryBo
     * @return
     * @folder 综合指标/分页查询综合指标列表
     */
    @GetMapping("/list")
    public TableDataInfo selectAggregativeIndicatorPageList(AggregativeIndicatorQueryBo queryBo) {
        startPage();
        TableVo<AggregativeIndicator, AggregativeIndicatorPageVo> data = aggregativeIndicatorService.selectAggregativeIndicatorPageList(queryBo);
        return getDataTable(data);
    }

    /**
     * 查询综合指标详情信息
     *
     * @param aggregativeIndicatorId
     * @return
     * @folder 综合指标/查询综合指标详情信息
     */
    @GetMapping("/{aggregativeIndicatorId}")
    public AjaxResult getAggregativeIndicatorInfo(@PathVariable("aggregativeIndicatorId") Long aggregativeIndicatorId) {
        AggregativeIndicatorVo aggregativeIndicatorVo = aggregativeIndicatorService.selectAggregativeIndicatorById(aggregativeIndicatorId);
        if (ObjectUtil.isEmpty(aggregativeIndicatorVo)) {
            return error("数据不存在!");
        }
        return success(aggregativeIndicatorVo);
    }

    /**
     * 综合指标
     *
     * @param bo
     * @return
     * @folder 综合指标/新增综合指标
     */
    @Log(title = "综合指标", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult addAggregativeIndicator(@RequestBody @Validated(AddGroup.class) AggregativeIndicatorBo bo) {
        return toAjax(aggregativeIndicatorService.addAggregativeIndicator(bo));
    }

    /**
     * 编辑综合指标
     *
     * @param bo
     * @return
     * @folder 综合指标/编辑综合指标
     */
    @PostMapping("/edit")
    @Log(title = "综合指标", businessType = BusinessType.UPDATE)
    public AjaxResult updateAggregativeIndicator(@RequestBody @Validated(EditGroup.class) AggregativeIndicatorBo bo) {

        return toAjax(aggregativeIndicatorService.updateAggregativeIndicator(bo));
    }

    /**
     * 删除综合指标
     *
     * @param idList
     * @return
     * @folder 综合指标/删除综合指标
     */
    @Log(title = "综合指标", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public AjaxResult remove(@RequestBody List<Long> idList) {
        return toAjax(aggregativeIndicatorService.removeAggregativeIndicator(idList));
    }

    /**
     * 下载综合指标模版
     *
     * @param response
     * @folder 综合指标/下载综合指标模版
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<AggregativeIndicatorImportVo> util = new ExcelUtil<>(AggregativeIndicatorImportVo.class);
        util.importTemplateExcel(response, "综合指标导入模版");
    }

    /**
     * 导入综合指标数据
     *
     * @param file
     * @return
     * @throws Exception
     * @folder 综合指标/导入综合指标数据
     */
    @Log(title = "收货订单", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<AggregativeIndicatorImportVo> util = new ExcelUtil<>(AggregativeIndicatorImportVo.class);
        List<AggregativeIndicatorImportVo> voList = util.importExcel(file.getInputStream());
        aggregativeIndicatorService.importAggregativeIndicator(voList);
        return success();
    }
}
